package com.jackrain.nea.fc.cp.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.util.TypeUtils;
import com.jackrain.nea.fc.cp.api.ViewCempQueryCmd;
import com.jackrain.nea.fc.cp.mapper.ViewCempMapper;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;

/**
 * 人员档案-接口服务
 *
 * @author WSL
 * @create 2017/10/30
 */
@Slf4j
@Component
@Service(protocol = "dubbo" , validation = "true" , version = "1.0" , group = "cp")
public class ViewCempQueryCmdImpl extends CommandAdapter implements ViewCempQueryCmd {
    @Override
    public ValueHolder execute(HashMap map) throws NDSException {
        // 常量
        String cpcDistribId = "CP_C_DISTRIB_ID";
        String cpcStoreId = "CP_C_STORE_ID";
        String cpcPostId = "CP_C_POST_ID";
        String cpcPostgradeId = "CP_C_POSTGRADE_ID";
        String sex = "SEX";
        String isLeave = "ISLEAVE";
        String cpcJobId = "CP_C_JOB_ID";
        String cpcJobRankId = "CP_C_JOBRANK_ID";

        ValueHolder vh = new ValueHolder();
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(map.get("param"),"yyyy-MM-dd HH-mm-ss",SerializerFeature.WriteMapNullValue), Feature.OrderedField);
        log.info("ViewCempQueryCmdImpl--param-->{}",param);

        if ((null == param) || (!param.containsKey("IDS")) || param.getJSONArray("IDS").size() <= 0) {
            throw new NDSException("参数格式错误！");
        }
        // Mapper
        ViewCempMapper viewCempMapper = ApplicationContextHandle.getBean(ViewCempMapper.class);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        // 根据param查询条件 查询【人员档案】表
        List<HashMap> list = viewCempMapper.selectProvider(param.getJSONArray("IDS").toArray());
        JSONArray resultJson = new JSONArray();
        for (HashMap hashMap: list) {
            Long objid = TypeUtils.castToLong(hashMap.get("ID"));

            // 判断【配销中心】是否为空 CP_C_DISTRIB_ID
            if (null != hashMap.get(cpcDistribId)){
                String eCode = viewCempMapper.selStoreorgEcode(TypeUtils.castToLong(hashMap.get(cpcDistribId)));
                hashMap.put(cpcDistribId,eCode);
            }
            // 判断【所属店仓】是否为空 CP_C_STORE_ID
            if (null != hashMap.get(cpcStoreId)){
                String eCode = viewCempMapper.selStoreEcode(TypeUtils.castToLong(hashMap.get(cpcStoreId)));
                hashMap.put(cpcStoreId,eCode);
            }
            // 判断【岗位】是否为空 CP_C_POST_ID
            if (null != hashMap.get(cpcPostId)){
                String eCode = viewCempMapper.selPostEcode(TypeUtils.castToLong(hashMap.get(cpcPostId)));
                hashMap.put(cpcPostId,eCode);
            }
            // 判断【岗位级别】是否为空 CP_C_POSTGRADE_ID
            if (null != hashMap.get(cpcPostgradeId)){
                String eCode = viewCempMapper.selPostgradeEcode(TypeUtils.castToLong(hashMap.get(cpcPostgradeId)));
                hashMap.put(cpcPostgradeId,eCode);
            }

            // 查询最大押金日期
            Long deductDate = viewCempMapper.selDeductDate(objid);
            // 插入 扣押金日期 DeductDate
            hashMap.put("DeductDate",deductDate);

            // 查询累计金额
            Double orderAmount = viewCempMapper.selOrderAmount(objid);
            // 插入 服装押金 OrderAmount
            hashMap.put("OrderAmount",orderAmount);

            // sex转换
            if (null != hashMap.get(sex)){
                String sexChar = hashMap.get("SEX").toString();
                if ("W".equals(sexChar)){
                    hashMap.put("SEX",0);
                }else if ("M".equals(sexChar)) {
                    hashMap.put("SEX", 1);
                }
            }

            // isleave转换
            if (null != hashMap.get(isLeave)){
                String sexChar = hashMap.get("ISLEAVE").toString();
                if ("N".equals(sexChar)){
                    hashMap.put("ISLEAVE",0);
                }else if ("Y".equals(sexChar)) {
                    hashMap.put("ISLEAVE", 1);
                }
            }
            // 下发人 MODIFIERID
            if (null != hashMap.get("MODIFIERID")){
                String modifyName = viewCempMapper.selModifyName(TypeUtils.castToLong(hashMap.get("MODIFIERID")));
                hashMap.put("MODIFIERID",modifyName);
            }
            // 是否转正 ISOFFICIAL
            if(null != hashMap.get("ISOFFICIAL")){
                String isOffical = hashMap.get("ISOFFICIAL").toString();
                if ("Y".equals(isOffical)){
                    hashMap.put("ISOFFICIAL",1);
                }else if ("N".equals(isOffical)){
                    hashMap.put("ISOFFICIAL",0);
                }
            }

            // 判断【职务】是否为空 cpcJobId
            if (null != hashMap.get(cpcJobId)){
                String eCode = viewCempMapper.selcpcJobEcode(TypeUtils.castToLong(hashMap.get(cpcJobId)));
                hashMap.put(cpcJobId,eCode);
            }

            // 判断【职等】是否为空 cpcJobRankId
            if (null != hashMap.get(cpcJobRankId)){
                String eCode = viewCempMapper.selcpcJobRankEcode(TypeUtils.castToLong(hashMap.get(cpcJobRankId)));
                hashMap.put(cpcJobRankId,eCode);
            }


            JSONObject jsonObject = JSON.parseObject(JSON.toJSONStringWithDateFormat(new JSONObject(hashMap),"yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
            resultJson.add(jsonObject);
        }
        vh.put("data",resultJson);
        return vh;
    }
}
